import types from './types'
import { ElMessage } from 'element-plus'

export default {
    [types.SET_STATE](state, params) {
        state[params.key] = params.value
    },
    [types.SET_ORDER_LIST](state, payload) {
        if (Array.isArray(payload)) {
            state.songOrderList = payload
            return
        }
        let ids = state.songOrderList.map(v => v.id) || []
        if (
            ids.includes(payload.id)
        ) {
            ElMessage.warning({
                showClose: true,
                message: '歌曲正在等待播放~'
            })
            return
        } else {
            state.songOrderList = [...state.songOrderList, payload]
        }
    },
    playNextSong(state) {
        let currentId = state.songInfo.id
        let index = state.songOrderList.findIndex((v) => {
            return v.songId == currentId
        })
        let nextSong = state.songOrderList[index + 1]
        console.log(nextSong)
        if (nextSong) {
            let { songId, ...rest } = nextSong
            let item = {
                ...rest,
                id: songId,
            }
            console.log(songId, item)
            state.songInfo = item
        } else {
            ElMessage.warning({
                showClose: true,
                message: '没有歌曲可以切了！'
            })
        }
    }

}
